<?php
class Blog extends Controller
{
	private $mod = null;		//model
	function __construct()
	{
		if (!$this->_check_login('user_id'))
		{
			header('Location: /admin/login.html');
			exit();
		}
		
		$this->mod = load_model('blog_model');
	}
	
	function getitems()
	{
		$inline = (isset($_GET['inline'])) ? 1 : 0;
		
		
		$items = array();
		$items = $this->mod->getItems();
		$count = count($items);
		parent::$tpl->assign('paging_pages', ceil($count / 14));
		parent::$tpl->assign('paging_ipp', 14);		// items per page
		parent::$tpl->assign('items', $items);
		
		if ($inline)
		{
			echo parent::$tpl->fetch('blog/list.tpl');
			exit();
		}
	}
	
	public function index()
	{
		$this->getitems();
		
		
		parent::$tpl->assign('content', parent::$tpl->fetch('blog/index.tpl'));
	}
	
	public function warnings()
	{
		
	}
	
	public function add()
	{
		$inline = (isset($_GET['inline'])) ? 1 : 0;
		
		if ($_POST['submit'])
		{
			if ($inline)
			{
				$data = array();
				parse_str($_POST['data'], $data);
			}
			else
			{
				foreach ($_POST as $key => $value)
				{
					$data[$key] = $value;
				}
			}
			
			if ($data['title'] && $data['body'])
			{
				//add to database
				$id = $this->mod->add($data);
				
				if ($inline)
				{
					$return = array();
					$return['id'] = $id;
					$return['callback'][] = 'updateBlogItems()';
					$return['callback'][] = 'addBlogItemList('.$id.')';
					echo json_encode($return);
					exit();
				}
				else
				{
					header('Location: /admin/blog/edit.html;id='.$id);
					exit();
				}
			}
			else
				parent::$tpl->assign('error', 1);
		}
		
		
		
		
		
		
		parent::$tpl->assign('inline', $inline);
		
		if (!$inline)
		{
			$this->getitems();
			parent::$tpl->assign('content', parent::$tpl->fetch('blog/add.tpl'));
		}
		else
		{
			echo parent::$tpl->fetch('blog/add.tpl');
			exit();
		}
	}
	
	
	function edit()
	{
		$inline = (isset($_GET['inline'])) ? 1 : 0;
		$id = (int) $_GET['id'];
		
		if ($_POST['submit'])
		{
			if ($inline)
			{
				$data = array();
				parse_str($_POST['data'], $data);
			}
			else
			{
				foreach ($_POST as $key => $value)
				{
					$data[$key] = $value;
				}
			}
			
			if ($data['title'] && $data['body'])
			{
				$this->mod->edit($data);
				
				if ($inline)
				{
					$return = array();
					$return['id'] = $data['id'];
					$return['callback'][] = 'updateBlogItems()';
					
					echo json_encode($return);
					exit();
				}
				else
				{
					header('Location: /admin/blog/edit.html;id='.$id);
					exit();
				}
			}
			else
				parent::$tpl->assign('error', 1);
		}
		
		
		
		parent::$tpl->assign('item', $this->mod->getItem($id));
		parent::$tpl->assign('inline', $inline);
		if (!$inline)
		{
			$this->getitems();
			parent::$tpl->assign('content', parent::$tpl->fetch('blog/edit.tpl'));
		}
		else
		{
			echo parent::$tpl->fetch('blog/edit.tpl');
			exit();
		}
	}
	
	function delete()
	{
		$inline = (isset($_GET['inline'])) ? 1 : 0;
		
		
		if ($_POST['submit'])
		{
			$ids = array();
			if ($inline)
			{
				$data = array();
				parse_str($_POST['data'], $data);
				$ids = array_values($data['deleteitem']);
			}
			else
				$ids = array_values($_POST['deleteitem']);
			
			$this->mod->delete($ids);
			
			if ($inline)
			{
				$return = array();
				$return['callback'][] = 'updateBlogItems()';
				
				echo json_encode($return);
				exit();
				
			}
			else
			{
				header('Location: /admin/blog/');
				exit();
			}
		}
	}
	
	
	
	
	
	function getitemjson()
	{
		$res = array();
		
		$res = $this->mod->getItem((int) $_GET['id']);
		
		echo json_encode($res);
		exit();
	}
	
	function getitemsjson()
	{
		$res = array();
		$res = $this->mod->getItems();
		
		echo json_encode($res);
		exit();
	}
	
	function __destruct(){}
}
?>